import com.entity.Book01;
import com.mapper01.Book01Mapper;

import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;


import javax.sql.DataSource;
import java.util.List;
import java.util.Properties;

/**
 * Created by webrx on 2017/6/23.
 */
public class Book {
    protected SqlSessionFactory sf;
    protected SqlSession ss;
    protected Book01Mapper book;
    @Before
    public void init() throws Exception {
        Properties p = new Properties();
        p.setProperty("driverClassName","com.mysql.jdbc.Driver");
        p.setProperty("url","jdbc:mysql://localhost:3306/store?useSSL=true&useUnicode=true&characterEncoding=UTF8");
        p.setProperty("username","root");
        p.setProperty("password","root");
        p.setProperty("maxIdle","10");
        p.setProperty("maxActive","20");
        p.setProperty("maxWait","-1");
        new BasicDataSourceFactory();
        DataSource ds= BasicDataSourceFactory.createDataSource(p);
       Environment env = new Environment("mysql", new JdbcTransactionFactory(), ds);
        this.sf  = new SqlSessionFactoryBuilder().build(new Configuration(env));

        this.sf.getConfiguration().addMappers("com.mapper01");// 添加所有接口
        this.ss= this.sf.openSession();
        book =this.ss.getMapper(Book01Mapper.class);
    }
    @After
    public void close(){
        ss.commit();
        this.ss.close();
    }

    @Test
    public void show(){
        System.out.println("你好");
         List<Book01> list= book.query();
         for(Book01 bo: list){
             System.out.println(bo.getName());
         }

         Book01 boo= new Book01();
         boo.setName("王五");
         boo.setPrice(200);
         int nn=book.add(boo);
        System.out.println(nn);
    }
}
